<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 数组去重
      // 1. set
      const arrs = [
        42,
        "apple",
        { name: "Alice", age: 30 },
        ["banana", "orange", "banana"],

        3.14,
        { name: "Bob", age: 25 },
        42,
        ["banana", "orange", "banana"],
      ];
      console.log(new Set(arrs));

      //   //   2. indexof
      //   function unique(val) {
      //     const newArr = [];
      //     for (let i = 0; i < val.length; i++) {
      //       if (newArr.indexOf(arrs[i]) === -1) {
      //         newArr.push(arrs[i]);
      //       }
      //     }
      //     return newArr;
      //   }
      //   console.log(unique(arrs));

      //3. includes
      function unique(val) {
        const newArr = [];
        for (let i = 0; i < val.length; i++) {
          if (!newArr.includes(arrs[i])) {
            newArr.push(arrs[i]);
          }
        }
        return newArr;
      }

      //4. filter
      //   function unique(arr) {
      //     const newarr = arr.filter((item, index, arrss) => {
      //       return arrss.indexOf(item, 0) === index;
      //     });

      //     return newarr;
      //   }
      //   console.log(arrs.indexOf(1, 2));
      //   console.log(unique(arrs));

      // 5 map
      let map = new Map();
      const tableData = [
        {
          name: "Tom1",
          tag: "Home",
        },
        {
          name: "Tom2",
          tag: "Office",
        },
        {
          name: "Tom1",
          tag: "Home",
        },
        {
          name: "Tom2",
          tag: "Office",
        },
        
      ];
      for (const item of tableData) {
        // console.log(item);
        if (!map.has(item.name)) {
          map.set(item.name, item);
        }
        // console.log(map);
      }
      let temparr = [...map.values()];
      console.log(temparr);

      // 6 set
      function uniqueSet(arr) {
        let set = new Set();
        return arr.filter((item) => {
          let str = JSON.stringify(item);
          if (set.has(str)) {
            return true;
          } else {
            set.add(str);
            return false;
          }
          return [...set.values()];
        });
      }

      console.log(uniqueSet(tableData));
    </script>
  </body>
</html>
